import asyncio

from rest_framework.views import APIView
from llm.entity.dto.DeviceConfigAddDTO import DeviceConfigAddDTO
from llm.service.VectorStoreService import VectorStoreService
from HomeAutoAI.common.utils.ThreadLocal import get_cur_user
from HomeAutoAI.common.Results import AsyncHttpResult, HttpResult
from llm.entity.vo.DeviceConfigAddVO import DeviceConfigAddVO


class AddDeviceConfig(APIView):
    def post(self, request):
        """
        添加新的设备并为其配置相关的配置信息，存储到数据库和vectorstore中，供后续的Assistant使用。
        #TODO: 新增设备有两个点，一个是存文件加入vectorstore并附加到Assistant中(这点适合单独设备的Assistant)，另一个是存入文件并附加到已有的全家助手中
        :return:
        """
        vo = DeviceConfigAddVO()
        try:
            dto = DeviceConfigAddDTO(**request.data)
            print(dto.json())
            user = get_cur_user()
            if user is None:
                raise Exception("未获取当前的用户信息")
            vo = VectorStoreService.attach_file(dto, user)
            print(vo.__dict__)
        except Exception as e:
            return HttpResult.error(message=str(e))
        return HttpResult.success(message="添加设备成功", data=vo.dict())
